#define _CRT_SECURE_NO_WARNINGS 1

#include "list.h"


LNode* buynode(int x)
{
	LNode* newnode = (LNode*)malloc(sizeof(LNode));
	if (newnode)
	{
		newnode->val = x;
		newnode->next = NULL;
	}
	return newnode;
}

void pushback(LNode* head, LNode* newnode)
{
	LNode* cur = head;
	while (cur->next)
	{
		cur = cur->next;
	}
	cur->next = newnode;
}

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
	int sumofdig = 0;
	int mod = 0;
	int addone = 0;
	LNode* cur1 = l1;
	LNode* cur2 = l2;
	LNode* head = buynode(0);
	while (cur1 && cur2)
	{
		sumofdig = cur1->val + cur2->val + addone;
		if (sumofdig >= 10)
		{
			addone = 1;
		}
		else
		{
			addone = 0;
		}
		mod = sumofdig % 10;
		LNode* newnode = buynode(mod);
		pushback(head, newnode);
		cur1 = cur1->next;
		cur2 = cur2->next;
	}
	if ((!(cur1 || cur2)) && (addone == 1))
	{
		LNode* newnode = buynode(1);
		pushback(head, newnode);
	}
	if (cur1)
	{
		LNode* tmp = cur1;
		while (cur1)
		{
			sumofdig = cur1->val + addone;
			if (sumofdig == 10)
			{
				addone = 1;
			}
			else
			{
				addone = 0;
				cur1->val = sumofdig;
				break;
			}
			cur1->val = 0;
			cur1 = cur1->next;
			if (cur1 == NULL && addone == 1)
			{
				LNode* newnode = buynode(1);
				pushback(tmp, newnode);
			}
		}
		pushback(head, tmp);
	}
	if (cur2)
	{
		LNode* tmp = cur2;
		while (cur2)
		{
			sumofdig = cur2->val + addone;
			if (sumofdig == 10)
			{
				addone = 1;
			}
			else
			{
				addone = 0;
				cur2->val = sumofdig;
				break;
			}
			cur2->val = 0;
			cur2 = cur2->next;
			if (cur2 == NULL && addone == 1)
			{
				LNode* newnode = buynode(1);
				pushback(tmp, newnode);
			}
		}
		pushback(head, tmp);
	}
	return head->next;
}

void stack_push(char* stack, char x, int* pcount)
{
	stack[(*pcount)++] = x;
}

void stack_pop(char* stack, int* pcount)
{
	stack[--(*pcount)] = '\0';
}

int maxDepth(char* s) 
{
	int count = 0;
	char* stack = (char*)calloc(101, sizeof(char));
	size_t len = strlen(s);
	int subofbracket = 0;
	while (subofbracket < len)
	{
		if (s[subofbracket] == '(')
		{
			stack_push(stack, s[subofbracket], &count);
		}
		if(s[subofbracket] == ')')
		{
			stack_pop(stack, &count);
		}
		subofbracket++;
	}
	return count;
}
